import qs from 'qs'
import Vue from 'vue'
import App from './App'
import router from './router'
// UI框架
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
// 公共less
import './assets/less/common.less'
// 公共js
import api from './assets/js/common.js'
import './assets/js/tb.js'

// 交互插件axios
import axios from 'axios'

const baseUrl = 'http://47.97.103.230:8080/shopping';

axios.defaults.baseURL = baseUrl; // 测试
sessionStorage.setItem('baseUrl', baseUrl);

axios.defaults.withCredentials = true;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 统一设置请求不成功的方法
axios.interceptors.response.use(response => {
	MintUI.Indicator.close();
	if(response.data.code != 0) {
		MintUI.Toast(response.data.msg || '请求出错');
	}
	if(response.data.cookieId) {
		sessionStorage.setItem('cookieId', response.data.cookieId)
	}
	return response.data;
}, error => {
	console.log(error);
});

// 统一设置请求前修改参数
axios.interceptors.request.use((config) => {
	MintUI.Indicator.open({
		text: '加载中...',
		spinnerType: 'fading-circle'
	});
	//	if(sessionStorage.getItem('cookieId')) {
	//		config.headers['Cookie'] = sessionStorage.getItem('cookieId');
	//	}
	config.data = qs.stringify(config.data);
	return config;
});

// 路由渲染前动作 -- 判断是否登录
router.beforeEach((to, from, next) => {
	if(api.arrIsHasItem(to.fullPath)) { // 判断是否不需要登录的path
		if(!sessionStorage.getItem('loginInfo')) {
			router.push({
				path: '/not_login'
			});
			return;
		}
	}
	next();
});

Vue.config.productionTip = false

Vue.prototype.api = api;
Vue.prototype.$http = axios;
// 使用插件
Vue.use(MintUI);

/* eslint-disable no-new */
new Vue({
	el: '#app',
	router,
	template: '<App/>',
	components: {
		App
	}
})